<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */
    
    /*********************************************************
    *            --== InArch ==--
    *
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @copyright essentialmind.com 2005
    *
    *********************************************************/

    /***
    * class ajaxAddNode
    ****
    * This class will create a new node via an AJAX method
    ****
    * @package ajax
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @copyright essentialmind.com 2005
    ***/
    class ajaxRatePage extends ajaxAction{

        public function process(){
            
            //see if I have already rated this page
            if(!$this->db->getOne("SELECT ip FROM page_ratings_history_anon WHERE pageid = ".(int)$_REQUEST['id']." AND pagetype = ".(int)$_REQUEST['type']." AND ip = ".$this->db->qstr(ip2long($_SERVER['REMOTE_ADDR'])))){
                $this->db->Execute("INSERT INTO page_ratings_history_anon(pageid, pagetype, ip) VALUES(".(int)$_REQUEST['id'].",".(int)$_REQUEST['type'].",".$this->db->qstr(ip2long($_SERVER['REMOTE_ADDR'])).")");
                                      
                //recalculate rating for the page
                if(!$this->db->getOne("SELECT votes FROM page_ratings WHERE pageid = ".(int)$_REQUEST['id']." AND pagetype = ".(int)$_REQUEST['type'])){
                    $this->db->Execute("INSERT INTO page_ratings(pageid, pagetype, votes, rating) VALUES(".(int)$_REQUEST['id'].",".(int)$_REQUEST['type'].",1,".(int)$_REQUEST['rate'].")");
                }else{
                    $this->db->Execute("UPDATE page_ratings SET votes = votes + 1, rating = rating + ".(int)$_REQUEST['rate']." WHERE pageid = ".(int)$_REQUEST['id']." AND pagetype = ".(int)$_REQUEST['type']);
                }
            }
                        
        }
    }
?>